﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Najak
{

	public interface IActivity
	{
		void Attach(Entity owner);
		bool Update(float timeDelta);
		void Abort();
	}

	public abstract class Activity : IActivity
	{
		public string Name;
		public Entity Owner;

		abstract protected bool OnUpdate(float timeDelta);
		public bool IsStarted;
		public bool Update(float timeDelta)
		{
			if (!IsStarted)
			{
				IsStarted = true;
				_OnStarted();
			}

			bool hasCompleted = OnUpdate(timeDelta);

			if (hasCompleted)
			{
				_OnEnded();
			}
			return hasCompleted;
		}

		virtual protected void _OnStarted()
		{
		}

		virtual protected void _OnEnded()
		{
		}

		virtual public void Attach(Entity owner)
		{
			Owner = owner;
		}

		public void Abort()
		{
			if (IsStarted)
				_OnEnded();
		}
	}
}
